package com.wx.nchu_mall.web;

import com.wx.nchu_mall.model.Cart;
import com.wx.nchu_mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    protected CartService cartService;

    @PostMapping("/api/cart/getCartlist")
    public List<Cart> getCartlist(@RequestBody Cart cart){
        return cartService.getCarlist(cart);
    }

    @PostMapping("/api/cart/update")
    public String update(@RequestBody Cart cart){
        cartService.update(cart);
        return "success";
    }

    @PostMapping("/api/cart/delete")
    public String delete(@RequestBody Cart cart){
        cartService.deleteById(cart);
        return "success";
    }

    @PostMapping("/api/cart/addCart")
    public String addCart(@RequestBody Cart cart){
        Cart cart1 = cartService.find(cart);
        if(cart1!=null){
            cart1.setQuantity(cart1.getQuantity()+1);
            cartService.update(cart1);
            return "002";
        }
        cartService.insert(cart);
        return "001";
    }

}
